-- SPDX-License-Identifier: GPL-3.0-or-later

--------------------------------------------------
--本回合造成多少伤害
--------------------------------------------------

local damage_checker_turn = fk.CreateTriggerSkill{
  name = "#damage_checker_turn",
  global = true,
  refresh_events = {fk.Damage},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local damage = data
    -- print(damage.damage)
    room:addPlayerMark(player, "#turn_damage-turn", damage.damage)
  end,
  }

-- Fk:addSkill(damage_checker_turn)

--------------------------------------------------
--本回合（被）某个角色指定为目标
--注释:
-- 在三国杀中，一名角色使用牌指定N个目标，将会产生N个“指定目标时”"指定目标后”时机。
-- 在Vup杀中，一名角色使用牌指定N个目标，只会产生1个“指定目标时”“指定目标后”时机。
-- 在三国杀中的“你使用牌指定一名角色为目标后”将会依次询问对每名目标是否发动技能。
-- 在Vup杀中，类似技能通常会对所有目标同时生效，或询问玩家要对哪名目标发动技能。
-- 因此targetspecified是多次分别指定所有目标，其他Vup杀技能在处理这个问题时需要携带data.firstTarget.
--------------------------------------------------

local target_specified_check = fk.CreateTriggerSkill{
  name = "#target_specified_check",
  global = true,
  --时机：目标确定后
  refresh_events = {fk.TargetSpecified},
  --触发条件：触发时机的角色为遍历到的角色
  can_refresh = function(self, event, target, player, data)
    return target == player and data.firstTarget
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local tars = AimGroup:getAllTargets(data.tos)
    local tar
    -- local pp
    local targets = {}
    local players = {}
    if player:getMark("#specified_target-turn") ~= 0 then
      targets = player:getMark("#specified_target-turn")
    end
    for _,p in ipairs(tars) do
      tar = room:getPlayerById(p)
      if tar:getMark("#target_specified-turn") ~= 0 then
        players = tar:getMark("#target_specified-turn")
      end
      table.insertIfNeed(players, player.id)
      -- pp = table.concat(players, "+")
      table.insertIfNeed(targets, tar.id)
      -- local tt = table.concat(targets, "+")
      --为目标的记录中增加本回合指定过自己的角色
      room:setPlayerMark(tar, "#target_specified-turn", players)
      --为遍历到的角色的记录中增加本回合已指定的目标
      room:setPlayerMark(player, "#specified_target-turn", targets)
      --（测试用）
      -- room:setPlayerMark(tar, "@target_specified-turn", pp)
      -- room:setPlayerMark(player, "@specified_target-turn", tt)
    end
    --测试用
    -- if player:getMark("@specified_target-turn") ~= {} then
    --   targets = player:getMark("@specified_target-turn")
    -- end
    -- if target:getMark("@target_specified-turn") ~= {} then
    --   players = target:getMark("@target_specified-turn")
    -- end
  end,
}

-- Fk:addSkill(target_specified_check)

--------------------------------------------------
--本局游戏中使用的总牌数。
--适用范围：三叹等。
--注释：根据三叹可以确认，Fk类的技能同时机优先级低于当前回合逆时针角色，其余不确定。
--------------------------------------------------

local card_use_checker = fk.CreateTriggerSkill{
  name = "#card_use_checker",
  global = true,
  --时机：卡牌使用宣告后
  -- refresh_events = {fk.CardUseFinished},
  refresh_events = {fk.AfterCardUseDeclared},
  can_refresh = function(self, event, target, player, data)
    return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "card_used", 1)
  end,
}

-- Fk:addSkill(card_use_checker)

--------------------------------------------------
--标记清理者。
--涉及：获得/失去技能时的标记清理（待定，会涉及无忌和袭穴），
--      离开手牌/进入弃牌堆/拥有者变化清理cardmark【在mark末尾前加上-cardmark!ldc*，ldc根据代码需要填写】。
--------------------------------------------------

local mark_cleaner = fk.CreateTriggerSkill{
  name = "#mark_cleaner",
  global = true,
  --时机：
  -- refresh_events = {fk.EventAcquireSkill, fk.EventLoseSkill, fk.AfterCardsMove},
  refresh_events = {fk.AfterCardsMove},
  --刷新时机：
  --卡牌移动后：
  --①离手
  --②进入弃牌堆
  --③持有者变化
  can_refresh = function(self, event, target, player, data)
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile 
          or (move.from and move.fromArea == Card.PlayerHand and move.toArea ~= Card.PlayerHand)
          or (move.from and move.to and move.from ~= move.to)
        then
          return true
        end
      end
    end
    -- return target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.AfterCardsMove then
      for _, p in ipairs(room:getAllPlayers()) do
				if p:getMark("v_Not_clear_card_flag") > 0 then return false end	--有Not_clear_card_flag这个flag存在时，不会清除cardflag
			end
      for _, move in ipairs(data) do
        --离手触发。
        if move.from and move.fromArea == Card.PlayerHand and move.toArea ~= Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            for name, _ in pairs(Fk:getCardById(info.cardId).mark) do
              local _,cms = name:find("-cm!")
              local cme,_ = name:find("*")
              if cms and cme and cms < cme then
                local cm = name:sub(cms, cme)
                if cm:find("l") then
                  room:setCardMark(Fk:getCardById(info.cardId), name, 0)
                end
              end
            end
          end
        end
        --进入弃牌堆触发。
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            for name, _ in pairs(Fk:getCardById(info.cardId).mark) do
              local _,cms = name:find("-cm!")
              local cme,_ = name:find("*")
              if cms and cme and cms < cme then
                local cm = name:sub(cms, cme)
                if cm:find("d") then
                  room:setCardMark(Fk:getCardById(info.cardId), name, 0)
                end
              end
            end
          end
        end
        --持有者变化触发。
        if move.from and move.to and move.from ~= move.to then
          for _, info in ipairs(move.moveInfo) do
            for name, _ in pairs(Fk:getCardById(info.cardId).mark) do
              local _,cms = name:find("-cm!")
              local cme,_ = name:find("*")
              if cms and cme and cms < cme then
                local cm = name:sub(cms, cme)
                if cm:find("c") then
                  room:setCardMark(Fk:getCardById(info.cardId), name, 0)
                end
              end
            end
          end
        end
      end
    end
  end,
}

--------------------------------------------------
--特殊机制：冰霜伤害
--------------------------------------------------

local v_ice_type_damage = fk.CreateTriggerSkill{
  --（刚需）技能认证名
  name = "v_ice_type_damage",
  global = true,
  -- mute = true
  --技能为锁定技，满足条件后强制发动
  frequency = Skill.Compulsory,
  --时机：造成伤害时
  events = {fk.DamageCaused},
  --触发条件：
  --（造成伤害时）触发时机的角色为遍历到的角色，
  --             造成的伤害大于0, 伤害种类为冰属性，
  --             伤害来源存在且在场。
  --             伤害目标存在且在场。
  --             来源可以弃置目标牌（看情况补，先补有牌）。
  can_trigger = function(self, event, target, player, data)
    --local room = player.room
    return 
      (data.damage > 0 and data.damageType == fk.IceDamage) 
      and target == player and player and player == data.from and player:isAlive()
      -- and data.from and data.from:isAlive()
      and data.to and data.to:isAlive()
      and not data.to:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local prompt = "#v_ice_type_damage_choice:"..data.to.id
    -- if room:askForSkillInvoke(data.from, self.name, data, prompt) then
    if room:askForSkillInvoke(player, self.name, data, prompt) then
      local room = player.room
      room:sendLog{
        type = "#v_ice_type_damage_log",
        from = data.from.id,
        --log.to是数组
        to = {data.to.id},
      }
      -- local card_id = room:askForCardChosen(data.from, data.to, "he", fk.ReasonDiscard)
      -- room:throwCard(card_id, self.name, data.to, data.from)
      local card_id = room:askForCardChosen(player, data.to, "he", self.name)
      room:throwCard(card_id, self.name, data.to, player)
      -- if data.from:isAlive() and data.to:isAlive() and not data.to:isNude() then
      if player:isAlive() and data.to:isAlive() and not data.to:isNude() then
        -- local card_id = room:askForCardChosen(data.from, data.to, "he", fk.ReasonDiscard)
        -- room:throwCard(card_id, self.name, data.to, data.from)
        local card_id = room:askForCardChosen(player, data.to, "he", self.name)
        room:throwCard(card_id, self.name, data.to, player)
      end
      --被防止的伤害仍为传导伤害。
      if data.chain and data.to.chained then
        room:sendLog{
          type = "#v_ice_type_damage_log2",
          --log.to是数组
          to = {data.to.id},
        }
        data.to:setChainState(false)
      end
      return true
    end
  end,
}

-- Fk:addSkill(v_ice_type_damage)

VslAuxSkills = {
  damage_checker_turn,
  target_specified_check,
  card_use_checker,
  v_ice_type_damage,
  mark_cleaner,
}

for _,i in ipairs(VslAuxSkills) do
  table.insert(AuxSkills, i)
end
